

//获取当前时间戳
function gettimestamp() {
  var timestamp = Date.parse(new Date());//单位毫秒
  timestamp = timestamp / 1000;
  console.log("当前时间戳为：" + timestamp);
  return timestamp;
}

function convertDateFromString(dateString) {
  if (dateString) {
    var arr1 = dateString.split(" ");
    var sdate = arr1[0].split('-');
    var _sdate = arr1[1].split(':');
    sdate.push(_sdate[0], _sdate[1], _sdate[2])
    var date = new Date(sdate[0], sdate[1] - 1, sdate[2], sdate[3], sdate[4], sdate[5]);//月份他就是要加一
    return date;
  }
}

function comparetime(update) {
  var ExpirationDate = new Date(update);
  ExpirationDate.setDate(ExpirationDate.getDate() + 1);//过期时间为1天
  // console.log('update'+ExpirationDate)
  var nowtime = new Date();
  // console.log('nowtime'+nowtime)
  if (nowtime > ExpirationDate) {
    // console.log('大，过期了')
    return 0
  }
  else if (nowtime < ExpirationDate) {
    // console.log('小,没过期')
    return 1
  }
  else {
    // console.log('err')
    return 2
  }
}

function cutting(info) {
  for (var item of info) {
    var data = item.time
    var str = data.split(' ')
    var time = str[1].substring(0, 5)
    item['time'] = time
    item['date'] = str[0]
  }
  return info
}

//用于将服务器的任务列表返回值按照时间分类！
function Standardization(info) {
  // console.log(info)
  var temp = [[{ date: '0' }]]//占位元素
  for (var info_item of info) {
    var lens = temp.length
    for (var i = 0; i < lens; i++) {
      var haved = 0;
      for (var temp_item of temp) {
        if (temp_item[0].date == info_item.date) {
          haved = haved + 1;
        }
      }
      // console.log(info_item.date+' '+temp[i][0].date)
      if (temp[i][0].date == info_item.date && haved > 0) {
        // console.log('1111')
        // console.log(temp[i])
        temp[i].push(info_item)
        break;
      }
      else if (haved == 0) {
        var make = []
        make.push(info_item)
        temp.push(make)
      }
    }
    // console.log('meici')
    // console.log(temp)
  }
  temp.shift()//去掉首个元素
  return temp
}

//需要传入最初从服务器获取的json
//直接保存在对象{}里面
function strTodate(info) {
  // var date = '2015-03-05 17:59:00.0';
  for (var item of info) {
    var date = item.time
    date = date.substring(0, 19);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    item["timestamp"] = timestamp
  }
}


// function pj(arr1,mid,arr3) {
//  try{
//   arr1.push(mid)
//  }
//  catch(err){
//    console.log(err)
//  }

//   for(var i=0;i<arr3.length;i++){
//     arr1.push(arr3[i])
//   }

// }
//快速排序实现时间线的正序排序
function sortBydate(info) {

  if (info.length < 2) {
    return info
  }
  var one_mid = info[parseInt(info.length / 2)]
  var mid = []
  mid.push(one_mid)
  console.log(mid)
  // console.log(mid)
  let left = []
  let right = []
  info.splice(info.length / 2, 1)
  for (var item of info) {
    if (item[0].timestamp >= mid[0][0].timestamp) {
      left.push(item)
    }
    else {
      right.push(item)
    }
  }
  return sortBydate(left).concat(mid, sortBydate(right))
}

function mychooseimage(that) {
  wx.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      that.setData({
        tempFilePaths: res.tempFilePaths,
        image_u:res.tempFilePaths[0],//前端加号显示的图片地址
        showpic:true
      })
      // console.log(that.data.image_u)
    },
    fail: () => { },
    complete: () => { }
  });
}

function sleep(d) {
  for (var t = Date.now(); Date.now() - t <= d;);
}

function upload(that) {
  const tempFilePaths = that.data.tempFilePaths
  console.log(that.data.inputValue)
  const uploadTask = wx.uploadFile({
    url: 'https://localhost:8080/file/', //仅为示例，非真实的接口地址
    filePath: tempFilePaths[0],
    name: 'file',
    formData: {
      'name': that.data.inputValue,
      'groupnum': that.data.info.groupnum,
      'event': that.data.event,
      'pwdWithSalt': that.data.pwdWithSalt
    },
    success(res) {
      var datastr = res.data
      var data = JSON.parse(datastr)
      if (data.errCode == 2020) {
        wx.showToast({
          title: '提交成功正在返回...',
          icon: 'none',
        });
        sleep(2000)
        wx.navigateBack({
          delta: 1
        })
      }
      else if (data.errCode == 2099) {
        wx.showToast({
          title: '容量不足',
          image: '../../pages/Personal_info/err.png',
        });
      }
      else {
        wx.showToast({
          title: '发生异常',
          image: '../../pages/Personal_info/err.png',
        });
      }
    },
    fail(res) {
      console.log(res)
    }
  })
  uploadTask.onProgressUpdate((res) => {
    that.setData({
      ifup: true,
      progress: res.progress.toString() + '%'
    })
    // console.log('上传进度', res.progress)
    // console.log('已经上传的数据长度', res.totalBytesSent)
    // console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  })
}

module.exports = {
  gettimestamp: gettimestamp,
  convertDateFromString: convertDateFromString,
  comparetime: comparetime,
  cutting: cutting,
  Standardization: Standardization,
  strTodate: strTodate,
  sortBydate: sortBydate,
  mychooseimage: mychooseimage,
  upload: upload
}